package org.distributed.transaction.core.manager;

import org.distributed.transaction.core.context.TransactionContext;
import org.distributed.transaction.core.exception.TransactionException;

/**
 * 事务管理器
 */
public interface TransactionManager {

	/**
	 * 开始分布式事务
	 *
	 * @param groupId 分布式事务
	 * @throws TransactionException TransactionException
	 */
	void begin(String groupId) throws TransactionException;

	/**
	 * 分布式事务成员加入
	 *
	 * @param transactionContext dtxTransaction
	 * @param unitId             unitId
	 * @param unitType           unitType
	 * @param modId              modId
	 * @param transactionState   transactionState
	 * @throws TransactionException TransactionException
	 */
	void join(TransactionContext transactionContext) throws TransactionException;

	/**
	 * 提交分布式事务。出错会记录异常记录
	 *
	 * @param transactionContext transaction
	 * @throws TransactionException TransactionException
	 */
	void commit(TransactionContext transactionContext) throws TransactionException;

	/**
	 * 回滚分布式事务。出错会记录异常记录
	 *
	 * @param transactionContext transaction
	 * @throws TransactionException TransactionException
	 */
	void rollback(TransactionContext transactionContext) throws TransactionException;

	/**
	 * 关闭分布式事务。出错会记录异常记录
	 *
	 * @param groupId groupId
	 */
	void close(String groupId);

	/**
	 * 获取事务状态（补偿机制）。出错返回-1
	 *
	 * @param groupId groupId
	 * @return transactionState
	 */
	int getTransactionState(String groupId);
}
